package com.mobileserver.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mobileserver.domain.BookClass;
import com.mobileserver.util.DB;

public class BookClassDAO {

	public List<BookClass> QueryAllBookClass() {
		List<BookClass> bookClassList = new ArrayList<BookClass>();

		DB db = new DB();
		String sql = "select * from BookClass";
		try {
			ResultSet rs = db.executeQuery(sql);
			while (rs.next()) {
				BookClass bookClass = new BookClass();
				bookClass.setBookClassId(rs.getInt("bookClassId"));
				bookClass.setBookClassName(rs.getString("bookClassName"));
				bookClassList.add(bookClass);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.all_close();
		}

		return bookClassList;

	}

	/* 传入图书类型对象，进行图书类型的添加业务 */
	public String AddBookClass(BookClass bookClass) {
		DB db = new DB();
		String result = "";
		try {
			/* 如果所有验证通过就执行sql插入新商品信息 */
			String sqlString = "insert into BookClass(bookClassName) values ('"
					+ bookClass.getBookClassName() + "')";
			db.executeUpdate(sqlString);
			result = "图书类别添加成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "图书类别添加失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 删除图书类别 */
	public String DeleteBookClass(String bookClassId) {
		DB db = new DB();
		String result = "";
		try {
			/* 如果所有验证通过就执行sql插入新商品信息 */
			String sqlString = "delete from BookClass where bookClassId="
					+ bookClassId;
			db.executeUpdate(sqlString);
			result = "图书类别删除成功!";
		} catch (Exception e) {
			System.out.println("数据库错误：" + e.getMessage()); 
			result = "图书类别删除失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 根据图书类别编号获取到图书类别信息 */
	public BookClass GetBookClass(int bookClassId) {
		BookClass bookClass = null;
		DB db = new DB();
		String sql = "select * from BookClass where bookClassId=" + bookClassId;
		try {
			ResultSet rs = db.executeQuery(sql);
			if (rs.next()) {
				bookClass = new BookClass();
				bookClass.setBookClassId(rs.getInt("bookClassId"));
				bookClass.setBookClassName(rs.getString("bookClassName"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return bookClass;
	}

	/* 更新图书类别信息 */
	public String UpdateBookClass(BookClass bookClass) {
		DB db = new DB();
		String result = "";
		try {
			String sql = "update BookClass set bookClassName='"
					+ bookClass.getBookClassName() + "' where bookClassId="
					+ bookClass.getBookClassId();
			db.executeUpdate(sql);
			result = "图书类别删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "图书类别删除失败";
		} finally {
			db.all_close();
		}
		return result;
	}
}
